﻿#region Librerias

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ESF.General.Text;
using System.Data;
using ESF.Data;
using ESF.General;

#endregion


namespace ESF.SFT.DataAccess
{
	public static class AdoFileRegisterValidator
	{
		#region Read

		public static List<FileRegisterValidator> GetValidators(FileRegisterValidatorSet pFileRegisterValidatorSet)
		{ 
			IDbCommand cmd;

			DbReader PISIS_Read = new DbReader("PISIS");


			cmd = PISIS_Read.CreateSPCommand("P_GetValidatorsBySchema");

			PISIS_Read.AddIDParameter(cmd, "pFileRegisterValidatorSetID", pFileRegisterValidatorSet.ID);

			return PISIS_Read.GetList<FileRegisterValidator>(cmd, ConvertTo);
		}
		

		private static FileRegisterValidator ConvertTo(IDataReader pReader, string pPrefix = "", string pListLoading = "")
		{
			return new FileRegisterValidator()
			{
				RegisterType = DataHelper.GetInt (pReader, "TipoRegistro"),
				Name = DataHelper.GetString (pReader, "Nombre"),
				Position = DataHelper.GetInt (pReader, "Posicion"),
				Length = DataHelper.GetInt (pReader, "Longitud"),
				IsIdentificationNumber = DataHelper.GetBool(pReader, "IsIdentificationNumber"),
				IsIdentificationTypeCode = DataHelper.GetBool(pReader, "IsIdentificationTypeCode"),
				IsRequired = DataHelper.GetBool(pReader, "Requerido", false),
				RegularExpression = pReader["ExpresionRegular"].ToString(),
				IsSequence = DataHelper.GetBool(pReader, "EsConsecutivo", false),
				SumRegisterType = DataHelper.GetNullableInt(pReader, "TipoRegistroAcumulado"),
				SumRegisterIndex = DataHelper.GetNullableInt(pReader, "PosicionCampoAcumulado"),
				AllowedValues = TextHelper.Split('|', DataHelper.GetString(pReader, "ValoresPermitidos")).ToArray(),
				IsRegisterCount = DataHelper.GetBool(pReader, "IsRegisterCount", false)
			};
		}

		#endregion

	}
}
